<template>
  <div class="app-container11">
    <el-container>
      <el-main>
        <div class="wf-query">
          <div class="wf-common-title" align="center">
            查询条件
          </div>
          <div style="padding-left: 10px">
            <el-form :inline="true" :model="formInline">
              <el-form-item label="模型名称">
                <el-input v-model="formInline.modelName" placeholder="请输入..."></el-input>
              </el-form-item>

              <el-form-item label="模型编号">
                <el-input v-model="formInline.key" placeholder="请输入..."></el-input>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
                <el-button type="primary" @click="dialog.dialogFormVisible = true">新增</el-button>
                <el-button type="primary" @click="dialog.dialogUploadVisible = true">导入部署文件</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>


        <el-table
          :data="processData"
          style="width: 100%">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="ID">
                  <span>{{ props.row.id }}</span>
                </el-form-item>
                <el-form-item label="模型编号">
                  <span>{{ props.row.key }}</span>
                </el-form-item>
                <el-form-item label="版本">
                  <span>{{ props.row.version }}</span>
                </el-form-item>
                <el-form-item label="元数据">
                  <span>{{ props.row.metaInfo }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column
            label="模型名称"
            prop="name"
          >
          </el-table-column>
          <el-table-column
            label="创建时间"
            prop="createTime"
            width="180"
            :formatter="formatCreated">
          </el-table-column>
          <el-table-column
            label="最后更新时间"
            prop="lastUpdateTime"
            width="180"
            :formatter="formatUpdated">
          </el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text"
                         @click="editModel(scope.row)">编辑
              </el-button>
              <el-button type="text"
                         @click="deploy(scope.row.id)">部署
              </el-button>
              <el-button type="text"
                         @click="exportBpmn(scope.row.id)">导出BPMN
              </el-button>
              <el-button type="text"
                         @click="exportJson(scope.row.id)">导出JSON
              </el-button>
              <el-button type="text"
                         @click="deleteModel(scope.row.id)">删除模型
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageable.currentPage"
          :page-sizes="[10, 50, 100]"
          :page-size="pageable.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageable.totalCount">
        </el-pagination>

        <el-dialog title="创建模型" :visible.sync="dialog.dialogFormVisible" center>
          <el-form :model="dialog.form" :rules="rules" ref="dialogForm" label-width="150px">
            <el-form-item label="名称" prop="name">
              <el-input v-model="dialog.form.name" placeholder="请输入模型名称"></el-input>
            </el-form-item>
            <el-form-item label="模型编号" prop="key" required>
              <el-input v-model="dialog.form.key" placeholder="请输入模型编号"></el-input>
            </el-form-item>
            <el-form-item label="所属系统" required prop="tenantId">
              <el-select v-model="dialog.form.tenantId" placeholder="请选择">
                <el-option label="请选择" value=""></el-option>
                <el-option
                  v-for="item in validSysOptions"
                  :key="item.sysKey"
                  :label="item.sysName"
                  :value="item.sysKey">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="描述" prop="description">
              <el-input type="textarea" v-model="dialog.form.description"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialog.dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm()">确 定</el-button>
          </div>
        </el-dialog>

        <el-dialog title="上传文件部署流程" :visible.sync="dialog.dialogUploadVisible" center>
          <el-form :model="dialog.upload">
            <el-form-item label="所属系统" required prop="tenantId">
              <el-select v-model="dialog.upload.tenantId" placeholder="请选择" @change="showUpload">
                <el-option label="请选择" value=""></el-option>
                <el-option
                  v-for="item in validSysOptions"
                  :key="item.sysKey"
                  :label="item.sysName"
                  :value="item.sysKey">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <el-upload v-show="dialog.upload.show"
                     class="upload-demo"
                     drag
                     :action="dialog.upload.action"
                     multiple
                     accept="text/xml,application/xml"
                     :on-error="uploadError"
                     :on-success="uploadSuccess">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传xml文件</div>
          </el-upload>
        </el-dialog>
      </el-main>
    </el-container>
  </div>
</template>

<script>
  import {fetchList, createModel, deleteModel,deployModel,exportModel} from '@/api/model'
  import {formatDate} from '@/utils/index'
  import {openWindow} from '@/utils/openWindow'

  export default {
    name: "modelManager",
    data() {
      return {
        pageable: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10,
        },
        formInline: {
          modelName: '',
          key: ''
        },
        processData: [],
        validSysOptions: [
          {
            sysKey: 102,
            sysName: '外星探测系统'
          }
        ],
        dialog: {
          dialogFormVisible: false,
          dialogUploadVisible: false,
          upload: {
            action: '',
            show: false,
            tenantId: ''
          },
          form: {
            description: '',
            key: '',
            name: '',
            tenantId: '',
          }
        },
        rules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'},
          ],
          tenantId: [{required: true, message: '请选择所属系统', trigger: 'blur'}]
        }
      }
    },
    methods: {
      loadData(data) {
        fetchList(data).then(response => {
          this.processData = response.data.data.list;
          this.pageable.totalCount = response.data.data.totalCount;
        })
      },
      reload() {
        let data = {
          pageSize: this.pageable.pageSize,
          pageNum: this.pageable.currentPage,
          key: this.formInline.key,
          name: this.formInline.name,
          close: this.formInline.hasHandUp
        };
        this.loadData(data);
      },
      onSubmit: function () {
        //重新加载数据
        var data = {
          pageSize: this.pageable.pageSize,
          pageNum: 1,
          modelName: this.formInline.modelName,

        };
        this.loadData(data);
        this.pageable.currentPage = 1;
      },
      handleSizeChange: function (val) {
        this.pageable.pageSize = val;
        this.pageable.currentPage = 1;
        //重新加载数据
        var data = {
          pageSize: val,
          pageNum: 1
        };
        this.loadData(data);
      },
      handleCurrentChange: function (val) {
        this.pageable.currentPage = val;
        //重新加载数据
        var data = {
          pageSize: this.pageable.pageSize,
          pageNum: val
        };
        this.loadData(data);
      },
      formatCreated: function (row, column) {
        return formatDate(new Date(row.createTime), 'yyyy-MM-dd HH:mm:ss');
      },
      formatUpdated: function (row, column) {
        return formatDate(new Date(row.lastUpdateTime), 'yyyy-MM-dd HH:mm:ss');
      },
      editModel: function (row) {
        window.open(`http://127.0.0.1:8080/activiti-modeler/modeler.html?modelId=${row.id}`,"_blank")
      },
      deleteModel: function (id) {
        this.$confirm('确定删除模型, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteModel(id).then(response => {
            this.reload();
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      deploy: function (modelId) {
        deployModel(modelId).then(response=>{
          this.$message({
            type: 'success',
            message: '部署成功!'
          });
        })
      },
      exportBpmn: function (id) {
        window.open("http://127.0.0.1:8080/model/export/" + id + "/bpmn", "_blank");
      },
      exportJson: function (id) {
        window.open("http://127.0.0.1:8080/model/export/" + id + "/json", "_blank");
      },
      submitForm: function () {
        let createData = {
          name: this.dialog.form.name,
          key: this.dialog.form.key,
          description: this.dialog.form.description,
          tenantId: this.dialog.form.tenantId
        };
        this.$refs['dialogForm'].validate((valid) => {
          if (valid) {
            createModel(createData).then(response => {
              this.dialog.dialogFormVisible = false;
              this.$refs["dialogForm"].resetFields();
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
              this.loadData({});
            });
          } else {
            return false;
          }
        });
      },
      showUpload: function (value) {
        if (!value) {
          this.dialog.upload.show = false;
        } else {
          this.dialog.upload.show = true;
          this.dialog.upload.action = "http://127.0.0.1:8080/model/deployByResource/" + value;
        }
      },
      uploadError: function (err, file, fileList) {
        this.$notify({
          title: '操作提醒',
          message: '部署失败',
          type: 'error'
        });
      },
      uploadSuccess: function (response, file, fileList) {
        this.$notify({
          title: '操作提醒',
          message: '部署成功',
          type: 'success'
        });
      }
    },
    created: function () {
      this.loadData();
    }
  }
</script>

<style scoped>
  .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand label {
    width: 80px;
    color: #99a9bf;
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
